﻿using PelitaProduksi.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PelitaProduksi.View.Panel.Master
{
    public partial class MasterKaryawan : Form
    {
        int max = 0;
        int highest = 0;
        Main m;
        int code;
        bool aktifOnly = false;
        List<int> change = new List<int>();
        List<Pegawai> list;
        public MasterKaryawan(Main m, int code)
        {
            InitializeComponent();
            init();
            this.m = m;
            this.code = code;
        }

        ~MasterKaryawan()
        {
            m.LIST_FORMS.Remove(code);
        }

        private void init()
        {
            this.list = Pegawai.listAllPegawai(aktifOnly);
            foreach (Pegawai p in this.list)
            {
                DataGridViewRow row = new DataGridViewRow();
                data.Rows.Add(p.Kode, p.Nama, p.Kelompok, p.Aktif, p.Keterangan);
                highest = int.Parse(p.Kode);
                if (max < highest) max = highest;
            }
        }

        private void Reset()
        {
            if(data!=null)
                data.Rows.Clear();
            init();
        }

        private bool Save()
        {
            try
            {
                List<Pegawai> list = new List<Pegawai>();
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    // Invalid input, ignore
                    if (data.Rows[i].Cells["kode"].Value == null) continue;
                    if (data.Rows[i].Cells["nama"].Value == null)
                    {
                        MessageBox.Show("Nama tidak boleh kosong");
                        return false;
                    }
                    string keterangan = null;
                    if (data.Rows[i].Cells["keterangan"].Value != null)
                    {
                        keterangan = data.Rows[i].Cells["keterangan"].Value.ToString();
                    }
                    Pegawai p = new Pegawai();
                    p.Kode = data.Rows[i].Cells["kode"].Value.ToString();
                    p.Nama = data.Rows[i].Cells["nama"].Value.ToString();
                    p.Kelompok = data.Rows[i].Cells["kelompok"].Value.ToString();
                    p.Keterangan = keterangan;
                    if (data.Rows[i].Cells["aktif"].Value != null)
                        p.Aktif = data.Rows[i].Cells["aktif"].Value.ToString() != "0";
                    else p.Aktif = false;
                    /* Check which row is changed */
                    foreach (Pegawai pp in this.list)
                    {
                        if (pp.Kode.Equals(p.Kode))
                        {
                            if (!pp.Equals(p))
                            {
                                list.Add(p);
                                break;
                            }
                        }
                    }
                }
                /* Query into database */
                foreach (Pegawai p in list)
                {
                    if (int.Parse(p.Kode) < highest)
                    {
                        // Edit
                        p.edit(p.Kode);
                    }
                    else
                    {
                        // Add
                        p.insert();
                    }
                }
                change.Clear();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Save
            if (Save())
            {
                MessageBox.Show("Data berhasil disimpan");
            }
            else
            {
                MessageBox.Show("Data Gagal disimpan", "Error Saving", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Reset
            Reset();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // Close
            this.Close();
        }

        private void data_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (data.CurrentRow.Cells["kode"].Value == null)
            {
                data.CurrentRow.Cells["kode"].Value = max++;
                data.CurrentRow.Cells["aktif"].Value = 1;
            }
        }

        private void MasterKaryawan_FormClosed(object sender, FormClosedEventArgs e)
        {
            m.Visible = true;
            m.Activate();
            m.BringToFront();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1 != null)
                aktifOnly = checkBox1.Checked;
            Reset();
            init();
        }
    }
}
